package com.haixiaoke.smartLock.yaluete;


import cn.hutool.core.convert.Convert;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.alibaba.fastjson2.JSON;
import com.haixiaoke.smartLock.tianwanghulian.SmartLockTwhlLogEventHandleService;
import com.haixiaoke.vo.TwhlDataVo;
import org.apache.commons.lang3.StringUtils;
import org.aspectj.apache.bcel.generic.IINC;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 亚略特-事件回调
 */
@Service
public class SmartLockYLTCallBackService {

    private static final Logger log = LoggerFactory.getLogger(SmartLockYLTCallBackService.class);
    @Autowired
    private SmartLockYLTEventService smartLockYLTEventService;

    public Map<String, Object> lockCallback(Map<String,Object> map) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("desc", "成功");
        result.put("code", 0);
        try {
            String pushType = Convert.toStr(map.get("pushType"));
            if (StrUtil.isBlank(pushType)) {
                log.warn("参数错误 vo:{}", JSONUtil.toJsonStr(map));
                return result;
            }
            Object content = map.get("pushContent");
            List<Map<String,Object>> list = Convert.convert(List.class, content);

            switch (pushType) {
                case "17":// 开锁日志
                    log.info("亚略特, 智能锁开锁日志回调------data={}", JSON.toJSONString(content));
                    smartLockYLTEventService.smartLockUnlockLog(list);
                    break;
                case "20":// 心跳日志
                    log.info("亚略特, 智能锁心跳日志回调------data={}", JSON.toJSONString(content));
                    smartLockYLTEventService.smartLockInfoLog(list);
                    break;
                case "21":// 告警日志
                    log.info("亚略特, 智能锁告警日志回调------data={}", JSON.toJSONString(content));
                    smartLockYLTEventService.smartLockWarningLog(list);
                    break;
                default:
                    log.warn("亚略特, 智能锁回调事件处理 = {}", pushType);
                    break;
            }

            return result;
        } catch (Exception e) {
            log.error("亚略特, 智能锁回调事件异常" + e.getMessage(), e);
            return result;
        }
    }
}
